package demo4;

import java.util.Arrays;
import java.util.Comparator;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: fzykd
 * Date: 2022-05-09
 * Time: 20:53
 */
class Student1 {//泛型 比较什么 要比较学生了
    public String name;
    public int age;
    public double score;

    public Student1(String name, int age, double score) {
        this.name = name;
        this.age = age;
        this.score = score;
    }
    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", score=" + score +
                '}';
    }

}

// 这就是比较器 对类的侵入性比较弱
class AgeComparator implements Comparator<Student1> { //根据age

    @Override
    public int compare(Student1 o1, Student1 o2) {
        return o1.age - o2.age;
    }
}

//如果要根据分数 在写个类 实现Comparator这个接口
class ScoreComparator implements Comparator<Student1>{
    @Override
    public int compare(Student1 o1, Student1 o2) {
        return (int) (o1.score-o2.score);
    }
}

public class TestTor {
    public static void main1(String[] args) {
        Student1 student1= new Student1("bit",12,98.9);
        Student1 student2= new Student1("haha",18,88.9);
        AgeComparator ageComparator = new AgeComparator();
        System.out.println(ageComparator.compare(student1,student2));
    }

    public static void main(String[] args) {
        Student1[] students = new Student1[3];
        students[0] = new Student1("bit",12,98.9);
        students[1] = new Student1("haha",18,88.9);
        students[2] = new Student1("heh",16,78.9);
        AgeComparator ageComparator = new AgeComparator();
        System.out.println(Arrays.toString(students));

        Arrays.sort(students,ageComparator);//不知道依据什么来排序 是name 还是age 还是score

        System.out.println(Arrays.toString(students));

    }
}